﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace StairsModuleAndEdit.StairsModel.CommonUnit
{
	internal class StairsUsingRangeXdata
	{
		private static Schema CreateSimpleSchema()
		{
			SchemaBuilder schemaBuilder = new SchemaBuilder(StairsUsingRangeXdata.m_Guid);
			schemaBuilder.SetReadAccessLevel((AccessLevel)1);
			schemaBuilder.SetSchemaName(StairsUsingRangeXdata.m_UsingRange);
			schemaBuilder.AddSimpleField(StairsUsingRangeXdata.m_UsingRange, typeof(string));
			return schemaBuilder.Finish();
		}

		private static bool HasSchema(out Schema schema)
		{
			Schema schema2 = Schema.Lookup(StairsUsingRangeXdata.m_Guid);
			schema = schema2;
			return schema2 != null;
		}

		public static void WriteStairsUsingRange(Element element, string strUsingRange)
		{
			Schema schema = null;
			if (!StairsUsingRangeXdata.HasSchema(out schema))
			{
				schema = StairsUsingRangeXdata.CreateSimpleSchema();
			}
			if (StairsUsingRangeXdata.m_StairsEnt == null)
			{
				StairsUsingRangeXdata.m_StairsEnt = new Entity(schema);
			}
			Field field = schema.GetField(StairsUsingRangeXdata.m_UsingRange);
			StairsUsingRangeXdata.m_StairsEnt.Set<string>(field, strUsingRange);
			element.SetEntity(StairsUsingRangeXdata.m_StairsEnt);
			StairsUsingRangeXdata.m_StairsEnt = null;
		}

		public static bool ReadStairsUsingRange(Element element, ref string strUsingRange)
		{
			Schema schema = null;
			if (StairsUsingRangeXdata.HasSchema(out schema))
			{
				Entity entity = element.GetEntity(schema);
				if (entity.IsValid())
				{
					Field field = schema.GetField(StairsUsingRangeXdata.m_UsingRange);
					if (field != null)
					{
						strUsingRange = entity.Get<string>(field);
						return true;
					}
				}
			}
			return false;
		}

		public static bool DeleteArchStairsEntity(Element element)
		{
			Schema schema = null;
			return StairsUsingRangeXdata.HasSchema(out schema) && element.DeleteEntity(schema);
		}

		private static Guid m_Guid = new Guid("23F4FA05-126C-441A-951F-3A78595E24AE");

		public static string m_UsingRange = "UsingRange";

		public static Entity m_StairsEnt = null;
	}
}
